//
//  AccountViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/5.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "AccountViewController.h"
#import "AccountCell.h"

@interface AccountViewController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UITableView *_tableView;
    NSDictionary *dataSource;
    UIView *headView;
    UIButton *confirmBtn;
    NSString *bankHolder;
    NSString *bankNumber;
    NSString *bankName;
}
@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createAccountView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MAccountInfo completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource = responseJson[@"data"];
            bankHolder = dataSource[@"bankHolder"];
            bankNumber = dataSource[@"bankNumber"];
            bankName = dataSource[@"bankName"];
            [_tableView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createAccountView
{
    self.titleLabel.text = @"支付信息";
    self.backButton.hidden = NO;

    [self createHeadView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = headView;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    _tableView.tableFooterView = footView;
    
    confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmBtn.layer.cornerRadius = 20;
    confirmBtn.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn setTitle:[NSString stringWithFormat:@"保存"] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [confirmBtn addTarget:self action:@selector(confirmBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:confirmBtn];
    confirmBtn.sd_layout
    .leftSpaceToView(footView, 50)
    .topSpaceToView(footView, 40)
    .widthIs(kScreenWidth - 100)
    .heightIs(40);
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 65)];
    headView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 55)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [headView addSubview:whiteView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 32, 21)];
    //    imageView.contentMode = UIViewContentModeScaleAspectFill;
    //    imageView.clipsToBounds = YES;
    imageView.image = [UIImage imageNamed:@"cm_bankcard"];
    [whiteView addSubview:imageView];
    
    UILabel *cardLabel = [[UILabel alloc] init];
    cardLabel.text = @"银行卡";
    cardLabel.font = [UIFont systemFontOfSize:15];
    cardLabel.textColor = [UIColor blackColor];
    [whiteView addSubview:cardLabel];
    cardLabel.sd_layout
    .topSpaceToView(whiteView, 10)
    .leftSpaceToView(imageView, 15)
    .widthIs(80)
    .heightIs(35);
}

-(void)confirmBtnHandler
{
    [self.view endEditing:YES];
    [[ToolNet sharedManager] changeAccountInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] bankHolder:bankHolder bankNumber:bankNumber bankName:bankName url:MAccountInfoChange completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [self.navigationController popViewControllerAnimated:YES];
            [TSMessage showMessage:@"修改成功" type:TSMessageNotificationTypeSuccess];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[AccountCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    NSArray *titleArr = @[@"开户人",@"卡号",@"开户行"];
    NSArray *holderArr = @[@"请填写开户人",@"请填写卡号",@"请填写开户行"];
    cell.titleLabel.text = titleArr[indexPath.row];
    cell.contentField.delegate = self;
    cell.contentField.placeholder = holderArr[indexPath.row];

    if (indexPath.row == 0) {
        cell.contentField.text = bankHolder;
    }
    else if (indexPath.row == 1) {
        cell.contentField.text = bankNumber;
    }
    else {
        cell.contentField.text = bankName;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma - UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    AccountCell *cell = (AccountCell *)[[textField superview] superview];
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if (indexPath.row == 0){
        bankHolder = textField.text;
    }
    else if (indexPath.row == 1){
        bankNumber = textField.text;
    }
    else if (indexPath.row == 2){
        bankName = textField.text;
    }
    [textField resignFirstResponder];
}

#pragma mark - UITextField Delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    AccountCell *cell = (AccountCell *)[[textField superview] superview];
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    if (indexPath.row == 0){
        bankHolder = textField.text;
    }
    else if (indexPath.row == 1){
        bankNumber = textField.text;
    }
    else if (indexPath.row == 2){
        bankName = textField.text;
    }
    [textField resignFirstResponder];

    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
